﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ywxt.NewsPush.Controllers
{
    using NHibernate;
    using NHibernate.Criterion;
    using NHibernate.Linq;
    using DB_NH;
    using ywxt.Common;

    [AllowAnonymous]
    public class NewsController : Controller
    {
        public ActionResult Index(int? pageIndex)
        {
            var _model = new Models.News.PageModelNewsIndex();

            if (pageIndex.HasValue)
            {
                _model.pageSetting.currentPage = pageIndex.Value;
            }

            var sessionFactory = SessionFactory.GetCurrentFactory();

            using (var session = sessionFactory.OpenSession())
            using (var transaction = session.BeginTransaction())
            {
                _model.pageSetting.totalItems= (from rs in session.Query<DB_NH.DB_Model.News.News>()
                                                where rs.IsValid == 1
                                                orderby rs.date descending
                                                select rs)
                               .Count();

                _model.News = (from rs in session.Query<DB_NH.DB_Model.News.News>()
                               where rs.IsValid == 1
                               orderby rs.date descending
                               select rs)
                               .Skip((_model.pageSetting.currentPage-1)*_model.pageSetting.pageSize)
                               .Take(_model.pageSetting.pageSize)
                               .ToList();
            }


            return View(_model);
        }

        public ActionResult About()
        {
            return View();
        }


    }
}
